﻿using Synergy.dbmodel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class FormZhijian : Form
    {
        string 生产订单号 = "";
        string 生产订单行号 = "";
        string 加工顺序 = "";
        string 工序编号 = "";
        string 工序名称 = "";
        int 报工id = 0;

        public FormZhijian()
        {
            InitializeComponent();
        }

        private void textBoxTm_KeyPress(object sender, KeyPressEventArgs e)
        {
            decimal 数量;
            string 物料编号;
            long 派工id;
           if (e.KeyChar == 13)
            {
                if (!pub.解析条码(this.textBoxTm.Text, out 生产订单号, out 生产订单行号, out 物料编号, out 数量, out 加工顺序, out 工序编号))
                {
                    MessageBox.Show("您扫描的条码没法解析，请重扫!", "提示", MessageBoxButtons.OK);
                    this.textBoxTm.SelectAll();
                    return;
                }
                List报工记录();
                List质检记录();

            }
        }

        private void List报工记录()
        {
            string sql;
            sql = @"  select id,生产订单号, 生产订单行号, 物料编号, 订单数量, 加工顺序, 工序编号, 工序名称, 报工数量, 工序单价, 
                开始时间, 结束时间, 报工工时, 工时单价, 金额, Z_UA_User.cuser_name 报工人员, 报工时间, 是否工时结算,'' 是否已检验 ,inventory.cinvaddcode 存货代码,
                inventory.cinvname 存货名称
                FROM      Z_baogong join inventory on  Z_baogong.物料编号=inventory.cinvcode join Z_UA_User  on Z_UA_User.cUSER_Id=z_baogong.报工人员 where 1=1 ";
            if (!this.checkBox包含已检验.Checked)
            {
                sql += " and id not in (select baogongid from z_zhijian where  baogongid is not null)";
            }
            if (生产订单号 != "")
            {
                sql += " and 生产订单号='" + 生产订单号 + "'";
            }
            if (生产订单行号 != "")
            {
                sql += " and 生产订单行号='" + 生产订单行号 + "'";
            }
            if (加工顺序 != "")
            {
                sql += " and 加工顺序='" + 加工顺序 + "'";
            }
            if (this.comboBox报工人员.Text != "请选择")
            {
                sql += " and 报工人员='" + this.comboBox报工人员.Text + "'";
            }
            if (this.comboBox工序名称.Text != "请选择")
            {
                sql += " and 工序名称='" + this.comboBox工序名称.Text + "'";
            }
            if (this.dateTimePicker_ksrq.Checked)
            {
                sql += " and 报工时间>='" + this.dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql += " and 报工时间<'" + this.dateTimePicker_jsrq.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            }

            sql += " order by  id asc ";
            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView1.AutoGenerateColumns = false;

            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                tb.Rows[i]["是否已检验"] = pub.Get是否已检验(tb.Rows[i]["id"].ToString());
            }

            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();

        }

        

        private void FormZhijian_Load(object sender, EventArgs e)
        {
            splitContainer1.SplitterDistance = this.Width / 2;
            comboBox报工人员Initl();
            comboBox工序名称Initl();
        }

        private void comboBox工序名称Initl()
        {
            string sql;
            sql = "select  distinct 工序名称 from Z_baogong   order by 工序名称 ";

            DataTable tb = pub.U8GetTable(sql);
            DataRow dr = tb.NewRow();
            dr["工序名称"] = "请选择";
            tb.Rows.InsertAt(dr, 0);
            comboBox工序名称.DataSource = tb;
            comboBox工序名称.DisplayMember = "工序名称";

        }

        private void comboBox报工人员Initl()
        {
            string sql;
            sql = "select  cUser_Name from Z_UA_User where  bCCUser is not  null order by cUser_Id ";

            DataTable tb = pub.SysGetTable(sql);
            DataRow dr = tb.NewRow();
            dr["cUser_Name"] = "请选择";
            tb.Rows.InsertAt(dr, 0);
            comboBox报工人员.DataSource = tb;

            comboBox报工人员.DisplayMember = "cUser_Name";


        }

        private void button刷新_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("004001"))
            {
            List报工记录();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name == "C检验")
                {
                    
                    DataGridViewRow gr = this.dataGridView1.Rows[e.RowIndex];
                    if (gr.Cells["C是否已检验"].Value.ToString() != "已检验")
                    {
                        this.textBox待检.Text = pub.ToDecimal(gr.Cells["C报工数量"].Value.ToString()).ToString();
                        this.textBox合格.Text = this.textBox待检.Text;                  
                        this.textBox不良.Text = "0";
                        工序名称 = gr.Cells["C工序名称"].Value.ToString();
                        报工id = pub.ToNum(gr.Cells["Cid"].Value.ToString());
                    }
                    else
                    {
                        this.textBox待检.Text = "";
                        this.textBox合格.Text = "";
                        this.textBox不良.Text = "";
                        报工id = 0;
                        MessageBox.Show("已经检验的，不用再检验！", "提示", MessageBoxButtons.OK);
                        return;
                    }
                }
            }
            catch
            {
                this.textBox待检.Text = "";
                this.textBox合格.Text = "";
                this.textBox不良.Text = "";
                报工id = 0;
            }

        }

        private void textBox不良_TextChanged(object sender, EventArgs e)
        {
            if (pub.ToDecimal(this.textBox合格.Text) >= 0 && pub.ToDecimal(this.textBox合格.Text) <= pub.ToDecimal(this.textBox待检.Text) && pub.ToDecimal(this.textBox不良.Text) <= pub.ToDecimal(this.textBox待检.Text))
            {
                this.textBox合格.Text = (pub.ToDecimal(this.textBox待检.Text) - pub.ToDecimal(this.textBox不良.Text)).ToString();
                //this.textBox不良.Text = (pub.ToDecimal(this.textBox待检.Text) - pub.ToDecimal(this.textBox合格.Text)).ToString();
            }
            else
            {
                MessageBox.Show("您输入的合格数量有问题！", "提示", MessageBoxButtons.OK);
                return;
            }
        }

        //private void textBox合格_TextChanged(object sender, EventArgs e)
        //{
        //    if (pub.ToDecimal(this.textBox合格.Text) >= 0 && pub.ToDecimal(this.textBox合格.Text) <= pub.ToDecimal(this.textBox待检.Text))
        //    {
        //        this.textBox合格.Text = (pub.ToDecimal(this.textBox待检.Text) - pub.ToDecimal(this.textBox不良.Text)).ToString();
        //        //this.textBox不良.Text = (pub.ToDecimal(this.textBox待检.Text) - pub.ToDecimal(this.textBox合格.Text)).ToString();
        //    }
        //    else
        //    {
        //        MessageBox.Show("您输入的合格数量有问题！", "提示", MessageBoxButtons.OK);
        //        return;
        //    }
        //}

        private void button确认_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("004002"))
            {
            decimal 合格数量 = pub.ToDecimal(this.textBox合格.Text);
            decimal 不良数量 = pub.ToDecimal(this.textBox不良.Text);
            decimal 待检数量 = pub.ToDecimal(this.textBox待检.Text);
                decimal 抽检数量 = 合格数量 + 不良数量;
                decimal 合格率 = Math.Round(合格数量 / (合格数量 + 不良数量), 2);
                string 不良原因 = "";

                List<Z_报工不良原因> z_报工不良原因List = new List<Z_报工不良原因>();
                if (不良数量 > 0)
                {
                    Form选择不良原因分类 form = new Form选择不良原因分类(工序名称, 不良数量);
                    form.ShowDialog();
                    if (form.DialogResult == DialogResult.OK)
                    {
                        if (z_报工不良原因List.Count > 0)
                        {
                            foreach (var item in z_报工不良原因List)
                            {
                                item.报工id = 报工id;
                            }
                            不良原因 = string.Join(";", z_报工不良原因List.Select(x => x.不良原因名称 + ":" + x.数量).ToList());
                            SqlSugarHelp.scope.Insertable(z_报工不良原因List).ExecuteCommand();
                        }
                        else
                        {
                            MessageBox.Show("不合格数量大于0，请选择不良原因");
                        }
                    }
                    else
                    {
                        MessageBox.Show("不合格数量大于0，请选择不良原因");
                        return;
                    }
                }

                if (报工id > 0 && 合格数量 >= 0 && 合格数量 <= 待检数量 && 合格数量 + 不良数量 == 待检数量)
            {
                    if (pub.Get是否已检验(报工id.ToString()) != "已检验")
                    {
                        string sql;
                        sql = @"insert into z_zhijian(baogongid, 合格数量,不良数量, 质检人员, 质检时间 )";
                        sql += "values('" + 报工id + "','" + 合格数量 + "','" + 不良数量 + "','"+ pub.LoginUser +"',getdate())";
                        pub.ExcuteSql(sql, "质检塞值");
                        sql = "update zj set zj.生产订单号 =bg.生产订单号, zj.生产订单行号=bg.生产订单行号, zj.物料编号=bg.物料编号,";
                        sql += " zj.订单数量=bg.订单数量, zj.加工顺序=bg.加工顺序, zj.工序编号=bg.工序编号, zj.工序名称=bg.工序名称 ";
                        sql += " from z_zhijian zj ,z_baogong bg where zj.baogongid=bg.id ";

                        string sqlUpdate质检 = @"update Z_baogong set 抽检数量 ="+ 抽检数量 + " ,合格数量 ="+ 合格数量 + " ,不合格数量 = "+ 不良数量 + ",合格率 = "+ 合格率 + ",不良原因 = '"+ 不良原因 + "' where id = " + 报工id ;
                        pub.ExcuteSql(sqlUpdate质检, "质检更新");
                        if (pub.ExcuteSql(sql, "质检更新") > 0)
                        {
                            报工id = 0;

                            List质检记录();
                            List报工记录();
                            MessageBox.Show("质检完成!", "提示", MessageBoxButtons.OK);
                            this.textBox待检.Text = "0";
                            this.textBox合格.Text = "0";
                            this.textBox不良.Text = "0";
                           
                        }
                       
                    }

            }
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
        }

        private void List质检记录()
        {
            string sql;
            sql = @"select zj.id, zj.生产订单号, zj.生产订单行号, zj.物料编号, zj.订单数量, zj.加工顺序, zj.工序编号, zj.工序名称, zj.baogongid, zj.合格数量, 
                zj.不良数量, zj.质检人员, zj.质检时间,bg.报工人员,bg.报工数量,bg.报工时间 ,Inventory.cinvname 存货名称
              FROM      Z_zhijian zj join z_baogong bg   ";
            sql += " on zj.baogongid=bg.id join Inventory on Inventory.cinvcode= zj.物料编号  where 1 =1 ";
             if (this.dateTimePickerZjksrq.Checked )
             {
                 sql += " and zj.质检时间>='" + this.dateTimePickerZjksrq.Value.ToString("yyyy-MM-dd") + "'";
             }
             if (this.dateTimePickerZjjsrq.Checked)
             {
                 sql += " and zj.质检时间<'" + this.dateTimePickerZjjsrq.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
             }
             sql += " and  zj.质检人员='" + pub.LoginUser + "'";

             if (this.textBox工号.Text!="")
             {
                 sql += " and bg.报工人员 like '%" + this.textBox工号.Text + "%'";
             }
             if (this.textBox存货名称.Text != "")
             {
                 sql += " and Inventory.cinvname like '%" + this.textBox存货名称.Text + "%'";
             }
            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView2.AutoGenerateColumns = false;
          

            this.dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            this.dataGridView2.DataSource = tb;

            this.dataGridView2.Refresh();
             
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("004003"))
            {
            List质检记录();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dataGridView2.Columns[e.ColumnIndex].Name == "D删除")
                {
                    if (MessageBox.Show("您要删除这条检验记录么?","询问",MessageBoxButtons.OKCancel)!=DialogResult.OK)
                    {
                        return;
                    }


                    ///删除
                    int id =   pub.ToNum(this.dataGridView2.Rows[e.RowIndex].Cells["Did"].Value.ToString());

                    string sql1 = "select 物料编号 from Z_zhijian where [id]='" + id + "' ";
                    DataTable tb1 = pub.U8GetTable(sql1);
                    string 物料编号删除 = tb1.Rows[0][0].ToString();
                    string sql2 = "select * from rdrecord10 join rdrecords10 on rdrecord10.id=rdrecords10.id where rdrecord10.cMPoCode='" + 生产订单号 + "' and rdrecords10.cinvcode='" + 物料编号删除 + "' ";
                    DataTable tb2 = pub.U8GetTable(sql2);
                    if (tb2.Rows.Count > 0)
                    {
                        MessageBox.Show("这个质检物料已经入库，需要删除产成品入库单才能删除！", "提示", MessageBoxButtons.OK);
                        return;
                    }
                    if (id > 0)
                    {
                        string sql = "delete Z_zhijian where id ='" + id + "'";
                        if (pub.ExcuteSql(sql,"删除质检记录")>0)
                        {
                            id = 0;
                            List质检记录();
                        }
                    }

                }
            }
            catch
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = null;
            this.dataGridView1.Refresh();
            ClearAll();
        }

        private void ClearAll()
        {
            this.textBoxTm.Text = "";
            this.textBoxTm.Focus();           
            this.checkBox包含已检验.Checked = false;          
            
        }

       
    }
}
